package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.AdvertPosition;
import com.ruoyi.domain.AdvertType;
import com.ruoyi.mapper.AdvertTypeMapper;
import com.ruoyi.service.IAdvertTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


/**
 * 广告信息类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
public class AdvertTypeServiceImpl implements IAdvertTypeService
{
    @Autowired
    private AdvertTypeMapper advertTypeMapper;

    /**
     * 查询广告信息类型
     * 
     * @param advertTypeId 广告信息类型主键
     * @return 广告信息类型
     */
    @Override
    public AdvertType selectAdvertTypeByAdvertTypeId(Long advertTypeId)
    {
        return advertTypeMapper.selectAdvertTypeByAdvertTypeId(advertTypeId);
    }

    /**
     * 查询广告信息类型列表
     * 
     * @param advertType 广告信息类型
     * @return 广告信息类型
     */
    @Override
    public List<AdvertType> selectAdvertTypeList(AdvertType advertType)
    {
        return advertTypeMapper.selectAdvertTypeList(advertType);
    }

    /**
     * 新增广告信息类型
     * 
     * @param advertType 广告信息类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAdvertType(AdvertType advertType)
    {
        int rows = advertTypeMapper.insertAdvertType(advertType);
        insertAdvertPosition(advertType);
        return rows;
    }

    /**
     * 修改广告信息类型
     * 
     * @param advertType 广告信息类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAdvertType(AdvertType advertType)
    {
        advertTypeMapper.deleteAdvertPositionByAdvertTypeId(advertType.getAdvertTypeId());
        insertAdvertPosition(advertType);
        return advertTypeMapper.updateAdvertType(advertType);
    }

    /**
     * 批量删除广告信息类型
     * 
     * @param advertTypeIds 需要删除的广告信息类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAdvertTypeByAdvertTypeIds(Long[] advertTypeIds)
    {
        advertTypeMapper.deleteAdvertPositionByAdvertTypeIds(advertTypeIds);
        return advertTypeMapper.deleteAdvertTypeByAdvertTypeIds(advertTypeIds);
    }

    /**
     * 删除广告信息类型信息
     * 
     * @param advertTypeId 广告信息类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAdvertTypeByAdvertTypeId(Long advertTypeId)
    {
        advertTypeMapper.deleteAdvertPositionByAdvertTypeId(advertTypeId);
        return advertTypeMapper.deleteAdvertTypeByAdvertTypeId(advertTypeId);
    }

    /**
     * 新增广告位置#advert_position信息
     * 
     * @param advertType 广告信息类型对象
     */
    public void insertAdvertPosition(AdvertType advertType)
    {
        List<AdvertPosition> advertPositionList = advertType.getAdvertPositionList();
        Long advertTypeId = advertType.getAdvertTypeId();
        if (StringUtils.isNotNull(advertPositionList))
        {
            List<AdvertPosition> list = new ArrayList<AdvertPosition>();
            for (AdvertPosition advertPosition : advertPositionList)
            {
                advertPosition.setAdvertTypeId(advertTypeId);
                list.add(advertPosition);
            }
            if (list.size() > 0)
            {
                advertTypeMapper.batchAdvertPosition(list);
            }
        }
    }
}
